package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.dao.RoomEslDao;
import io.renren.entity.RoomEslEntity;
import io.renren.service.RoomEslService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("roomEslService")
public class RoomEslServiceImpl extends ServiceImpl<RoomEslDao, RoomEslEntity> implements RoomEslService {

    @Autowired
    private RoomEslDao roomEslDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String eslId = (String) params.get("eslId");
        String type = (String) params.get("type");
        Long roomId = (Long) params.get("roomId");


        IPage<RoomEslEntity> page = this.page(
                new Query<RoomEslEntity>().getPage(params),
                new QueryWrapper<RoomEslEntity>()
                        .eq(eslId != null, "esl_id", eslId)
                        .eq(type != null, "type", type)
                        .eq(roomId != null, "room_id", roomId)
        );

        return new PageUtils(page);
    }

    public List<RoomEslEntity> queryByRoomIdAndType(Long roomId, String type) {

        QueryWrapper<RoomEslEntity> queryWrapper = new QueryWrapper<RoomEslEntity>()
                .eq(roomId != null, "room_id", roomId)
                .eq("type", type);

        List<RoomEslEntity> roomEslEntities = roomEslDao.selectList(queryWrapper);

        return roomEslEntities;
    }

    public List<RoomEslEntity> queryByType(String type) {

        QueryWrapper<RoomEslEntity> queryWrapper = new QueryWrapper<RoomEslEntity>()
                .eq("type", type);

        List<RoomEslEntity> roomEslEntities = roomEslDao.selectList(queryWrapper);

        return roomEslEntities;
    }

    public List<RoomEslEntity> queryByRoomId(Long roomId) {

        QueryWrapper<RoomEslEntity> queryWrapper = new QueryWrapper<RoomEslEntity>()
                .eq(roomId != null, "room_id", roomId);

        List<RoomEslEntity> roomEslEntities = roomEslDao.selectList(queryWrapper);

        return roomEslEntities;
    }

    public RoomEslEntity queryByEslId(String eslId) {

        QueryWrapper<RoomEslEntity> queryWrapper = new QueryWrapper<RoomEslEntity>()
                .eq(eslId != null, "esl_id", eslId);

        return roomEslDao.selectOne(queryWrapper);

    }

    public int countByRoomIdAndType(Long roomId, String type) {

        QueryWrapper<RoomEslEntity> queryWrapper = new QueryWrapper<RoomEslEntity>()
                .eq(roomId != 0, "room_id", roomId)
                .eq(type != null, "type", type);

        return roomEslDao.selectCount(queryWrapper);

    }

}
